package com.jt.aop;

import com.jt.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.jws.soap.SOAPBinding;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.Map;

/**
 * @outhor fufeixiang
 * @date 2021-04-27
 */
@Component
@Aspect
public class SpringAOP {
    private static Map<Integer, User> map = new HashMap<>();

    @Around("execution(* com.jt.service..*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = null;
        //获取目标对象方法的参数
        Object[] args = joinPoint.getArgs();
        //获取user对象
        User user = (User) args[0];

        //判断map集合中是否有数据  user的ID是唯一标识
        //获取user的id属性
        Integer id = user.getId();
        if(map.containsKey(id)){

            //map中有数据
            System.out.println("执行AOP缓存");
            //将获取的数据返回
            return map.get(id);
        } else{
            //执行目标方法
            result = joinPoint.proceed();
            //将user对象存放在map集合中
            map.put(id,user);
            System.out.println("AOP执行目标方法");

        }
        System.out.println("环绕通知");
        return result;
    }
}
